package org.dromara.patrol.service.impl;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.patrol.domain.PatrolReportDetail;
import org.dromara.patrol.domain.bo.PatrolReportDetailBo;
import org.dromara.patrol.domain.vo.PatrolReportDetailVo;
import org.dromara.patrol.mapper.PatrolReportDetailMapper;
import org.dromara.patrol.service.IPatrolReportDetailService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 巡店管理-巡店报告动态详情Service业务层处理
 *
 * @author samuel
 * @date 2024-03-27
 */
@RequiredArgsConstructor
@Service
public class PatrolReportDetailServiceImpl implements IPatrolReportDetailService {

    private final PatrolReportDetailMapper baseMapper;

    /**
     * 查询巡店管理-巡店报告动态详情
     */
    @Override
    public PatrolReportDetailVo queryById(Long detailId){
        return baseMapper.selectVoById(detailId);
    }

    /**
     * 查询巡店管理-巡店报告动态详情列表
     */
    @Override
    public TableDataInfo<PatrolReportDetailVo> queryPageList(PatrolReportDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PatrolReportDetail> lqw = buildQueryWrapper(bo);
        Page<PatrolReportDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询巡店管理-巡店报告动态详情列表
     */
    @Override
    public List<PatrolReportDetailVo> queryList(PatrolReportDetailBo bo) {
        LambdaQueryWrapper<PatrolReportDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PatrolReportDetail> buildQueryWrapper(PatrolReportDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PatrolReportDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(ObjUtil.isNotNull(bo.getDetailId()), PatrolReportDetail::getDetailId, bo.getDetailId());
        return lqw;
    }

    /**
     * 新增巡店管理-巡店报告动态详情
     */
    @Override
    public Boolean insertByBo(PatrolReportDetailBo bo) {
        PatrolReportDetail add = MapstructUtils.convert(bo, PatrolReportDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDetailId(add.getDetailId());
        }
        return flag;
    }

    /**
     * 修改巡店管理-巡店报告动态详情
     */
    @Override
    public Boolean updateByBo(PatrolReportDetailBo bo) {
        PatrolReportDetail update = MapstructUtils.convert(bo, PatrolReportDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PatrolReportDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除巡店管理-巡店报告动态详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
